package com.study.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mysql.cj.PreparedQuery;
import com.study.domain.User;
import com.study.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.jar.JarOutputStream;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/queryUser.do")
    @ResponseBody
    public PageInfo queryUser(Integer pageNumber){//page是查询的页数
      //调用分页查询方法，传入页码和每页记录数
        PageHelper.startPage(pageNumber,8);
        List<User> users = userService.selectUsers();
        //包装查询后的结果，将pageInfo交给页面
        //封装了详细的分页信息，包括查询出来的数据，传入连续现实的页数
        PageInfo<User> pageInfo=new PageInfo<User>(users,5);
        return pageInfo;
    }
    @ResponseBody
    @RequestMapping("/userAdd.do")
    public String userAdd(User user){
        String temp="";
        int i=userService.insertUser(user);
        if(i==1){
            temp="success";
        }else{
            temp="field";
        }
        return temp;
    }
    @ResponseBody
    @RequestMapping("/delUser.do")
    public Integer deleteUser(Integer uid){
        int i = userService.deleteUser(uid);
        return i;
    }

    @ResponseBody
    @RequestMapping(value="/editUser.do",method = RequestMethod.POST)
    public Integer editUser(User user){
        System.out.println(user);
        int i = userService.modifyUser(user);
        return i;
    }

}
